package br.ufpb.di.ppgi.negocio.modelo.professorExterno;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.validator.Email;
import org.hibernate.validator.NotEmpty;

import br.ufpb.di.ppgi.persistencia.listeners.ProfessorExternoListener;

@Entity
@EntityListeners(ProfessorExternoListener.class)
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"cpf"}))
public class ProfessorExterno implements Serializable {
	private static final long serialVersionUID = -2413867412331721599L;

	@Id @GeneratedValue
	private Long id;
	@NotEmpty @Email
	private String email;
	@NotEmpty
	private String nome;
	@NotEmpty
	private String cpf;
	private String matriculaSIAPE;
	@NotEmpty
	private String instituicaoDeOrigem;
	private String banco;
	private String conta;
	private String agencia;
	@Temporal(TemporalType.TIMESTAMP)
	private Date dataDeCadastro;
	
	public ProfessorExterno() {
		super();
	}

	public ProfessorExterno(Long id, String email, String nome, String cpf,
			String matriculaSIAP, String instituicaoDeOrigem, String banco,
			String conta, String agencia, Date dataDeCadastro) {
		super();
		this.id = id;
		this.email = email;
		this.nome = nome;
		this.cpf = cpf;
		this.matriculaSIAPE = matriculaSIAP;
		this.instituicaoDeOrigem = instituicaoDeOrigem;
		this.banco = banco;
		this.conta = conta;
		this.agencia = agencia;
		this.dataDeCadastro = dataDeCadastro;
	}

	public String getBanco() {
		return banco;
	}

	public void setBanco(String banco) {
		this.banco = banco;
	}

	public String getConta() {
		return conta;
	}

	public void setConta(String conta) {
		this.conta = conta;
	}

	public String getAgencia() {
		return agencia;
	}

	public void setAgencia(String agencia) {
		this.agencia = agencia;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getCpf() {
		return cpf;
	}

	public void setCpf(String cpf) {
		this.cpf = cpf;
	}

	public String getMatriculaSIAPE() {
		return matriculaSIAPE;
	}

	public void setMatriculaSIAPE(String matriculaSIAP) {
		this.matriculaSIAPE = matriculaSIAP;
	}

	public String getInstituicaoDeOrigem() {
		return instituicaoDeOrigem;
	}

	public void setInstituicaoDeOrigem(String instituicaoDeOrigem) {
		this.instituicaoDeOrigem = instituicaoDeOrigem;
	}

	public Date getDataDeCadastro() {
		return dataDeCadastro;
	}

	public void setDataDeCadastro(Date dataDeCadastro) {
		this.dataDeCadastro = dataDeCadastro;
	}
	
    public boolean equals(Object other) {
        if (other == this) return true;
        if (getId() == null && getCpf() == null)
        	return false;
        if (!(other instanceof ProfessorExterno)) return false;
        
        ProfessorExterno o = (ProfessorExterno)other;
        
        EqualsBuilder eq = new EqualsBuilder();
        
    	if (getId() != null && o.getId() != null) 
    		return eq.append(getId(), o.getId()).isEquals();
        	
    	return eq.append(getCpf(), o.getCpf()).isEquals();
    }
    
    
    public int hashCode() {
    	if (getId() == null && getCpf() == null) return super.hashCode();
    	
        HashCodeBuilder hash = new HashCodeBuilder(3,13);
        return getId() == null ? 
        		hash.append(getCpf()).toHashCode() : 
        		hash.append(getId()).toHashCode();
    }
	
	/*
	@Override
	public int hashCode() {
		if (cpf == null)
			return super.hashCode();
		
		final int prime = 97;
		int result = 1;
		result = prime * result + ((cpf == null) ? 0 : cpf.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (cpf == null)
			return false;
		if (!(obj instanceof ProfessorExterno))
			return false;

		ProfessorExterno other = (ProfessorExterno) obj;
		return cpf.equals(other.cpf);
	}
	*/
}
